﻿Imports Mystery

Public Class MultiReferenceSelection


    Private delete_bitmap As New BitmapImage(New Uri("/MysteryControls;component/Images/DeleteRed.png", UriKind.RelativeOrAbsolute))

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub


    Private _value As ICollection(Of IContent) = New List(Of IContent)

    Private Sub deleteSelection(selection As IContent)
        Dim current_value As New List(Of IContent)(_value)
        current_value.Remove(selection)
        Me.value = current_value
    End Sub

    Public Property value As IEnumerable(Of IContent)
        Get
            Return _value
        End Get
        Set(new_value As IEnumerable(Of IContent))

            If new_value.sameContents(_value) Then Exit Property

            If new_value Is Nothing Then
                _value = New List(Of IContent)
            Else
                _value = New List(Of IContent)(new_value)
            End If

            'interfance
            myPanel.Children.Clear()

            For Each new_content As IContent In _value
                If new_content Is Nothing Then Continue For
                Dim sp As New StackPanel With {.Orientation = Orientation.Horizontal}
                Dim cb As New Button
                Dim icb As IContentButton = new_content.GetType.getMysteryAttribute(Of ContentButtonAttribute)().createButton(new_content)
                AddHandler cb.Click, AddressOf icb.Press
                cb.Content = icb.getText
                cb.MinWidth = 100
                Dim image As Image = icb.getImage
                image.Height = 30
                image.Width = 30
                sp.Children.Add(image)
                sp.Children.Add(cb)

                Dim delete As New DeleteIcon
                Dim current_content As IContent = new_content

                delete.todo = Sub() deleteSelection(current_content)

                sp.Children.Add(delete)

                myPanel.Children.Add(sp)
            Next
            RaiseEvent valueChanged()
        End Set
    End Property

    Private _content As IContent


    Private Sub ContentSelected(content As IContent)
        Dim current As New List(Of IContent)
        current.addRange(value)
        current.Add(content)
        Me.value = current
    End Sub

    Private _controler As IContentSelectionControler

    Public Sub setWithType(Of T As IContent)()
        _controler = New ContentSelectionControler(Of T)
    End Sub

    Private Sub bAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles bAdd.Click
        Dim w As New ContentSelectionWindow
        w.Controler = _controler
        AddHandler w.contentSelected, AddressOf ContentSelected
        Dim screen_coo As Point = bAdd.PointToScreen(New Point)
        w.Left = screen_coo.X
        w.Top = screen_coo.Y
        w.ShowDialog()
        RemoveHandler w.contentSelected, AddressOf ContentSelected
    End Sub

    Public Event valueChanged()
End Class
